<?php

class CA_Content
{
	private $id = 0;
	
	private $rows = array(
		"title as title",
		"content as text",
		"create_date as create_date",
		"last_update as last_update"
	);
	
	public function whatToDo()
	{
		$GLOBALS['hc']->registerHook("CA_CONTENT_GET");
		
		$this->id = 1;
		
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$this->id = $_GET['id'];
		}
	}
	
	public function getPageContent()
	{
		$data = $this->fetch($this->id);
		
		if(!$data)
		{
			$GLOBALS['tplBody']->assign("content_title", "404 - Page not found");
			$GLOBALS['tplBody']->assign("content_text", "The page could not be found.");
			
			$GLOBALS['tplHead']->assign("page_title", $GLOBALS['tplHead']->get("page_title")." - Page not found");
			
			return false;
		}
		
		foreach ($data as $var => $val)
		{
			$GLOBALS['tplBody']->assign("content_".$var, $val);
			
			if($var == "title")
			{
				$GLOBALS['tplHead']->assign("page_title", $GLOBALS['tplHead']->get("page_title")." - ".$data['title']);
			}
		}
		
		return true;
	}
	
	private function fetch($id)
	{
		$sql = &$GLOBALS['sql'];
		
		$q = $sql->query("SELECT " . implode(", ", $this->rows) . " FROM content WHERE id = '" . $sql->escape($id) . "' AND active != 0");
		
		if(!$q)
		{
			return false;
		}
		
		if($sql->numRows($q) != 1)
		{
			return false;
		}
		
		$arr = $sql->fetchArray($q, MYSQL_ASSOC);
		
		$sql->freeResult($q);
		
		return $arr;
	}
}

$CA_Content = new CA_Content();

?>